package com.bwie.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.Goods;
import com.bwie.pojo.Indent;
import com.bwie.pojo.Refund;
import com.bwie.service.GoodsService;
import com.bwie.service.IndentService;
import com.bwie.service.RefundService;
import com.bwie.unit.QRCodeUtil;
import com.bwie.unit.R;
import com.bwie.unit.WxPayUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("list")
public class GoodsController {


    @Autowired
    private GoodsService goodsService;

    @Autowired
    private IndentService indentService;

    @Autowired
    private RefundService refundService;


    @Autowired
    private WxPayUtils wxPayUtils;
    //列表分页
    @PostMapping("goodsList")
    public R goodsList(@RequestBody Goods goods){
        Page<Goods> goodsPage = new Page<>(goods.getPageNum(), goods.getPageSize());
        Page<Goods> page = goodsService.page(goodsPage);
        return R.OK(page);
    }

    //
    @PostMapping("updateGoods")
    public R updateGoods(@RequestBody Goods goods,@RequestHeader(name = "token") String token){
        //修改商品数量
        goodsService.UpdateNums(goods.getGoodsNums());
        //修改商品价格
        goodsService.updatePrice(goods.getGoodsPrice());
        Indent indent = new Indent();
        //订单商品编号
        indent.setGoodsId(Math.toIntExact(goods.getId()));
        //订单商品名称
        indent.setGoodsName(goods.getGoodsName());
        //订单商品数量
        indent.setIndentNums(goods.getGoodsNums());
        //订单商品下单时间
        indent.setIndentTime(new Date());
        //订单商品价格
        indent.setGoodsPrice(goods.getGoodsPrice());
        //订单单号
        String code = RandomUtil.randomNumbers(4);
        indent.setIndentNumber("bwie"+code);
        //增加登陆人
        Claims body = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();
        Integer uid = body.get("uid", Integer.class);
        indent.setIndentUid(uid);
        //保存订单表
        indentService.save(indent);
        //删除商品
        goodsService.removeById(goods.getId());
        return R.OK("下单成功");
    }

    @PostMapping("indentList")
    public R indentList(@RequestHeader(name = "token")String token){
        Claims body = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();
        Integer uid = body.get("uid", Integer.class);
        Integer rid = body.get("rid", Integer.class);
        if (rid!=0){
           List<Indent> selectList = indentService.IndentList(uid);
           return R.OK(selectList);
        }else {
            List<Indent> selectIndentList = indentService.selectIndentList();
            return R.OK(selectIndentList);
        }
    }

    @PostMapping("echoIndent/{id}")
    public R echoIndent(@PathVariable Integer id){
        Indent indent = indentService.selectIndentById(id);
        return R.OK(indent);
    }

    @PostMapping("refundList")
    public R refundList(@RequestBody Indent indent){
        Refund refund = new Refund();
        refund.setRefundNums(indent.getIndentNums());
        refund.setRefundPicture(indent.getIndentPicture());
        refund.setRefundName(indent.getGoodsName());
        refund.setRefundPrice(indent.getGoodsPrice());
        refundService.save(refund);
        return R.OK("退换货成功,等待审批");
    }


    @GetMapping("wxPay/{orderId}")
    public void wxPay(@PathVariable String orderId, HttpServletResponse response){
        String codeUrl = wxPayUtils.wxPay(orderId);
        System.out.println(codeUrl);
        QRCodeUtil.createQRCode(response,codeUrl);
    }


    @PostMapping("refundService")
    public R refundService(){
        List<Refund> list = refundService.list();
        return R.OK(list);
    }

}
